<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>hello_react</title>
</head>
<body>
    <div id="test1"></div>
    <div id="test2"></div>
    <div id="test3"></div>

    <!-- 引入 react 核心库 -->
    <script src="./js/react.development.js" type="text/javascript"></script> <!-- React 对象 -->
    <script src="./js/react-dom.development.js" type="text/javascript"></script> <!-- ReactDOM 对象 -->
    <script src="./js/babel.min.js" type="text/javascript"></script> <!-- babel -->
    <script src="./js/prop-types.js" type="text/javascript"></script> <!-- PropTypes -->

    <script type="text/babel">
        class Person extends React.Component {
            render() {
                const {name, age, sex} = this.props
                return (
                    <ul>
                        <li>姓名：{name}</li>    
                        <li>性别：{sex}</li> 
                        <li>年龄：{age+1}</li>
                    </ul>
                )
            }
        }
        // 对 props 进行约束
        // 在 react16后弃用了React对象上的 PropToypes(导致React对象太臃肿),需单独引入 prop-types.js
        // 注意区分 propTypes 与 PropTypes （首字母大小写）
        Person.propTypes = {
            name: PropTypes.string.isRequired,
            sex: PropTypes.string,
            age: PropTypes.number,
            speak: PropTypes.func
        }
        Person.defaultProps = {
            sex: '女',
            age: 18
        }

        // age 传的非字符串值需要使用{}
        ReactDOM.render(<Person name="jerry" age={19} speak={speak}/>, document.querySelector('#test1'))
        ReactDOM.render(<Person name="tom" sex="男"/>, document.querySelector('#test2'))

        const p = { name: '老刘', age: 18, sex: '女'}
        ReactDOM.render(<Person {...p}/>, document.querySelector('#test3'))

        function speak() {
            console.log('我说话了')
        }
    </script>
</body>
</html>